//
//  TransBuyNowAlertController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/2.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "TransBuyNowAlertController.h"

@interface TransBuyNowAlertController ()
@property (weak, nonatomic) IBOutlet UIView *bottomBgView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UILabel *saleCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalMoneyLabel;

@property (assign, nonatomic)NSInteger goodsCount;// 商品数量
@property (assign, nonatomic)double totalMoney;// 合计
@end

@implementation TransBuyNowAlertController

- (instancetype)init {
    if (self = [super initWithNibName:nil bundle:nil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.goodsCount = 1;
    self.totalMoney = self.model.price.doubleValue;
}
- (void)setupUI{
     self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    // 切上边两个圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bottomBgView.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bottomBgView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.bottomBgView.layer.mask = maskLayer;
    CGFloat radius = 10;
    self.bottomBgView.layer.cornerRadius = radius;
    // 上面的方法只能将左上角切圆角，下面的方法是切右上角圆角
    if (@available(iOS 11.0, *)) {
        self.bottomBgView.layer.maskedCorners = kCALayerMaxXMinYCorner;
    } else {
        // Fallback on earlier versions
    }
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.model.img] placeholderImage:[UIImage imageNamed:@""]];
    self.titleLabel.text = self.model.name;
    self.typeLabel.text = self.model.number;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",self.model.price];
    self.saleCountLabel.text = [NSString stringWithFormat:@"销量：%@",self.model.num];
    
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"合计：¥%.2f",self.model.price.doubleValue];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.totalMoneyLabel.text];
    NSRange range1 = [[str string] rangeOfString:@"合计："];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range1];
    NSRange range2 = [[str string] rangeOfString:[NSString stringWithFormat:@"¥%.2f",self.model.price.doubleValue]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#FF7321"] range:range2];
    self.totalMoneyLabel.attributedText = str;
}
// 减
- (IBAction)reductionButtonClick:(UIButton *)sender {
    if (self.goodsCount == 1) {
        return;
    }
    self.goodsCount--;
    self.countLabel.text = [NSString stringWithFormat:@"%ld",self.goodsCount];
    double total = self.model.price.doubleValue * self.goodsCount;
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"合计：¥%.2f",total];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.totalMoneyLabel.text];
    NSRange range1 = [[str string] rangeOfString:@"合计："];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range1];
    NSRange range2 = [[str string] rangeOfString:[NSString stringWithFormat:@"¥%.2f",total]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#FF7321"] range:range2];
    self.totalMoneyLabel.attributedText = str;
    
    self.totalMoney = total;
}
// 加
- (IBAction)addButtonClick:(UIButton *)sender {
    
    self.goodsCount++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld",self.goodsCount];
    double total = self.model.price.doubleValue * self.goodsCount;
    
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"合计：¥%.2f",total];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.totalMoneyLabel.text];
    NSRange range1 = [[str string] rangeOfString:@"合计："];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range1];
    NSRange range2 = [[str string] rangeOfString:[NSString stringWithFormat:@"¥%.2f",total]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#FF7321"] range:range2];
    self.totalMoneyLabel.attributedText = str;
    
    self.totalMoney = total;
}
- (IBAction)buyNowButtonClick:(UIButton *)sender {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:getMyBalance param:param success:^(id dic) {
        
        NSString *balance = dic[@"data"];
        if (balance.doubleValue >= self.totalMoney) {// 可用余额大于等于合计金额 能买
            if (self.canBuyClick) {
                self.canBuyClick(self.totalMoney,balance,self.countLabel.text);
            }
        } else {// 余额不足
            if (self.noMoneyClick) {
                self.noMoneyClick(self.totalMoney, balance);
            }
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}

- (IBAction)cancelButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
